local love = require 'love'

local setColor = love.graphics.setColor
local drawRectangle = love.graphics.rectangle
local line = love.graphics.line
local setLineWidth = love.graphics.setLineWidth

local width = Width
local height = Height
local padding = Padding

-- 弃牌按钮使用绿色
local discardButton = {
    x = 0,
    y = 0,
    width = 0,
    height = 0,
    normalColor = {0.298, 0.686, 0.314, 1.0}, -- #4CAF50(草绿色)
    hoveredColor = {0.4, 0.733, 0.416, 1.0}, -- #66BB6A(淡草绿)
    pressedColor = {0.219, 0.556, 0.235, 1.0}, -- #388E3C(深绿色)
    currentColor = {},
    isHovered = false,
    isPressed = false
}

local trashIcon = {
    x = 0, y = 0, size = 0, w = 0, h = 0,
    top = {x = 0, y = 0, w = 0, h = 0},
    mainBody = {x = 0, y = 0, w = 0, h = 0, r = 0},
    cap = {x1 = 0, y1 = 0, x2 = 0, y2 = 0, x2r = 0, y2r = 0, x2current = 0, y2current = 0}
}

local function rotatePoint(x, y, cx, cy, angle)
    local rad = math.rad(angle)
    local cos = math.cos(rad)
    local sin = math.sin(rad)
    local nx = cx + (x - cx) * cos - (y - cy) * sin
    local ny = cy + (x - cx) * sin + (y - cy) * cos
    return nx, ny
end

local function init()
    local circleRadius = height / 20
    local widthButton = circleRadius
    local heightButton = circleRadius

    discardButton.x = width / 2 + widthButton
    discardButton.y = height - padding * 2 - heightButton
    discardButton.width = widthButton
    discardButton.height = heightButton

    discardButton.currentColor = discardButton.normalColor

    trashIcon.x = discardButton.x + discardButton.width / 2
    trashIcon.y = discardButton.y + discardButton.height / 1.8
    trashIcon.size = discardButton.height * 0.7

    trashIcon.w = trashIcon.size * 0.6  -- 宽度
    trashIcon.h = trashIcon.size * 0.6  -- 高度

    trashIcon.top.x = trashIcon.x - trashIcon.w / 2
    trashIcon.top.y = trashIcon.y - trashIcon.h / 2 - trashIcon.size * 0.15
    trashIcon.top.w = trashIcon.w
    trashIcon.top.h = trashIcon.size * 0.1

    trashIcon.mainBody.lineWidth = trashIcon.size * 0.08
    trashIcon.mainBody.x = trashIcon.x - trashIcon.w/2
    trashIcon.mainBody.y = trashIcon.y - trashIcon.h/2
    trashIcon.mainBody.w = trashIcon.w
    trashIcon.mainBody.h = trashIcon.h
    trashIcon.mainBody.r = trashIcon.size * 0.1

    trashIcon.cap.x1 = trashIcon.x - trashIcon.w/2 - trashIcon.size*0.05
    trashIcon.cap.y1 = trashIcon.y - trashIcon.h/2 - trashIcon.size*0.15
    trashIcon.cap.x2 = trashIcon.x + trashIcon.w/2 + trashIcon.size*0.05
    trashIcon.cap.y2 = trashIcon.y - trashIcon.h/2 - trashIcon.size*0.15

    trashIcon.cap.x2r, trashIcon.cap.y2r = rotatePoint(trashIcon.cap.x2, trashIcon.cap.y2, trashIcon.cap.x1, trashIcon.cap.y1, -28)
end

-- 绘制回收站图标
local function drawTrashIcon()
    -- 垃圾桶顶部
    setColor(0.1, 0.1, 0.1, 1)
    drawRectangle("fill", trashIcon.top.x, trashIcon.top.y, trashIcon.top.w, trashIcon.top.h)

    -- 垃圾桶主体
    setLineWidth(trashIcon.mainBody.lineWidth)
    drawRectangle("line", trashIcon.mainBody.x, trashIcon.mainBody.y, trashIcon.mainBody.w, trashIcon.mainBody.h, trashIcon.mainBody.r, trashIcon.mainBody.r)

    -- 垃圾桶盖子
    line(trashIcon.cap.x1, trashIcon.cap.y1, trashIcon.cap.x2current, trashIcon.cap.y2current)
end

local function drawButton()
    -- 更新按钮颜色
    if discardButton.isPressed then
        discardButton.currentColor = discardButton.pressedColor
        trashIcon.cap.x2current = trashIcon.cap.x2r
        trashIcon.cap.y2current = trashIcon.cap.y2r
    elseif discardButton.isHovered then
        discardButton.currentColor = discardButton.hoveredColor
        trashIcon.cap.x2current = trashIcon.cap.x2r
        trashIcon.cap.y2current = trashIcon.cap.y2r
    else
        discardButton.currentColor = discardButton.normalColor
        trashIcon.cap.x2current = trashIcon.cap.x2
        trashIcon.cap.y2current = trashIcon.cap.y2
    end

    -- 绘制按钮
    setColor(discardButton.currentColor)
    drawRectangle("fill", discardButton.x, discardButton.y, discardButton.width, discardButton.height, 10, 10)

    -- 绘制按钮边框
    setColor(0.2, 0.2, 0.2, 1)
    setLineWidth(1)
    drawRectangle("line", discardButton.x, discardButton.y, discardButton.width, discardButton.height, 10, 10)

    -- 绘制回收站图标
    drawTrashIcon()
end

local function inArea(x, y)
    return x >= discardButton.x and x <= discardButton.x + discardButton.width and y >= discardButton.y and y <= discardButton.y + discardButton.height
end

local function isHovered(boolean)
    discardButton.isHovered = boolean
end

local function isPressed()
    discardButton.isPressed = true
end

local function isReleased()
    discardButton.isPressed = false
end

return{
    init = init,
    drawButton = drawButton,
    inArea = inArea,
    isHovered = isHovered,
    isPressed = isPressed,
    isReleased = isReleased
}